import downloader.Downloader;
import parser.Parser;
import parser.Content;
import storage.Storage;
import store.Store;
import util.MyReflectionUtil;
import util.ProfileReader;

import java.util.List;
import java.util.Properties;

public class App {
    public static void main(String[] args) throws Exception {
        System.out.println("===== ===== =====");
        //读取配置文件
        System.out.println("配置文件读取中");
        Properties properties = ProfileReader.readProfile("src/config.properties");
        System.out.println("配置文件读取成功");
        System.out.println("===== ===== =====");

        //下载模块
        System.out.println("网页内容下载中");
        Downloader downloader = getDownloader(properties.getProperty("downloader"));
        String html = downloader.downloader(properties);
        System.out.println("网页内容下载完成");
        System.out.println("===== ===== =====");

        //解析模块
        System.out.println("网页内容解析中");
        Parser parser = getParser(properties.getProperty("parser"));
        List<Content> contentList = parser.parser(html, properties);
        System.out.println("网页内容解析完成");
        System.out.println("===== ===== =====");

        //存储模块
        System.out.println("网页内容存储中");
        Storage storage = getStorage(properties.getProperty("storage"));
        storage.storage(contentList);
        System.out.println("网页内容存储完成");
        System.out.println("===== ===== =====");

        //通知模块
        System.out.println("网页内容通知中");
        Store Store = getStore(properties.getProperty("store"));
        boolean store = Store.store(contentList, properties);
        if (store)
            System.out.println("网页内容通知完成");
        else System.out.println("未命中关键词");
        System.out.println("===== ===== =====");
    }

    private static Store getStore(String store) {
        return MyReflectionUtil.getInstance(store);
    }

    private static Storage getStorage(String storage) {
        return MyReflectionUtil.getInstance(storage);
    }

    private static Downloader getDownloader(String downloader) {
        return MyReflectionUtil.getInstance(downloader);
    }

    private static Parser getParser(String parser) {
        return MyReflectionUtil.getInstance(parser);
    }

}
